<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------

namespace app\common\logic\member;

use \think\Db;

class LevelLogic
{

    /**
     * User: Simony
     * Function:自动升级
     * @param $member_id
     * @return array|bool
     */
    public function upgrade($member_id)
    {
        $memberModel = model('common/member/Member');

        $member = $memberModel->getInfoByID($member_id);
        if (!$member) return ds_arr(ERROR, '当前用户不存在');

        $levelModel = model('common/member/Level');

        $level = $member['real_verify'] ? $levelModel::VERIFIY_LEVEL : $levelModel::ORDINARY_LEVEL;
        if ($member['sex'] == 1) { //男
            $packageBuyLogModel = model('common/package/PackageBuyLog');
            $where = [
                ['member_id', 'eq', $member_id],
                ['state', 'eq', 1],
                ['expire_end_time', 'gt', time()],
            ];
            $list = $packageBuyLogModel->getList($where, 1, '*', 'package_id desc');
            $level = $list ? $levelModel::VIP_LEVEL : $level; //套餐存在升级vip
        }


        $member['level'] != $level && $memberModel->setValByID($member_id, 'level', $level); //更新等级
        return true;
    }


}